﻿using Globalization;
using System;
using System.Collections.Generic;
using System.IO;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Storage;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The User Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234236

namespace JalaliCalendar
{
    public sealed partial class MonthControl : UserControl
    {
        DayControl[,] myTexts = new DayControl[5, 7];

        public MonthControl(int year, int month, int offset, int length)
        {
            this.InitializeComponent();

            InitTextBlocks(year, month, offset, length);
        }

        private void InitTextBlocks(int year, int month, int offset, int length)
        {
            MonthName.Text = Tasks.CalendarConversion.getMonthStrInPersianStatic(month);

            for (int ii = 0; ii < 7; ii++)
            {
                DayControl dc = new DayControl();
                dc.SetText(Tasks.CalendarConversion.getWeekDayStrInPersianStatic((ii + 7 - 2) % 7), 12);
                Grid.SetColumn(dc,ii);
                DaysGrid.Children.Add(dc);
            }

            Tasks.CalendarConversion cc = new Tasks.CalendarConversion();
            cc.setGregorianDate(DateTime.Today.Year, DateTime.Today.Month, DateTime.Today.Day);
            int todayYear = cc.getIranianYear();
            int todayMonth = cc.getIranianMonth();
            int todayDay = cc.getIranianDay();

            HijriCalendar hj = new HijriCalendar();
            hj.HijriAdjustment = (int)ApplicationData.Current.LocalSettings.Values["HijriAdjustment"];
            bool showHolidays = (bool)ApplicationData.Current.LocalSettings.Values["ShowHolidays"];

            int cellCount = 0;
            int dayCount = 1;
            for (int ii = 0; ii < 6; ii++)
            {
                for (int jj = 0; jj < 7; jj++)
                {
                    if (ii < 5)
                    {
                        myTexts[ii, jj] = new DayControl();
                        Grid.SetRow(myTexts[ii, jj], ii);
                        Grid.SetColumn(myTexts[ii, jj], jj);
                        MainGrid.Children.Add(myTexts[ii, jj]);
                    }
                    
                    if (cellCount >= offset && dayCount <= length)
                    {
                        myTexts[ii % 5, jj].SetText(dayCount.ToString());
                        bool isHoliday = false;
                        string holidayDesc = "";

                        // Persian Holidays
                        foreach (Holiday ho in Holidays.getHolidays())
                            if (ho.Type == HolidayType.Persian &&
                                ho.Month == month + 1 &&
                                ho.Day == dayCount)
                            {
                                isHoliday = true;
                                holidayDesc = ho.Description;
                            }

                        // Hijri Holidays
                        cc.setIranianDate(year, month + 1, dayCount);
                        DateTime dt = new DateTime(cc.getGregorianYear(), cc.getGregorianMonth(), cc.getGregorianDay());
                        int hijriMonth = hj.GetMonth(dt);
                        int hijriDay = hj.GetDayOfMonth(dt);
                        foreach (Holiday ho in Holidays.getHolidays())
                            if (ho.Type == HolidayType.Hijri &&
                                ho.Month == hijriMonth &&
                                ho.Day == hijriDay)
                            {
                                isHoliday = true;
                                holidayDesc = ho.Description;
                            }

                        // Fridays
                        if (jj == 6)
                            isHoliday = true;

                        // Today Color
                        if (todayYear == year &&
                            todayMonth == month + 1 &&
                            todayDay == dayCount)
                            myTexts[ii % 5, jj].SetColor(Colors.LightGreen);

                        if (showHolidays && isHoliday)
                        {
                            myTexts[ii % 5, jj].SetColor(Colors.LightSteelBlue);
                            if (holidayDesc != "")
                                ToolTipService.SetToolTip(myTexts[ii % 5, jj], holidayDesc);
                        }

                        dayCount++;
                    }
                    cellCount++;
                }
            }
        }
    }
}
